package com.Endurancer.manager.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import com.Endurancer.common.core.domain.AjaxResult;
import com.Endurancer.common.utils.DateUtils;
import com.Endurancer.manager.domain.BlogPost;
import com.Endurancer.manager.domain.Comment;
import com.Endurancer.manager.domain.dto.LikesDto;
import com.Endurancer.manager.domain.vo.LikesVo;
import com.Endurancer.manager.mapper.BlogPostMapper;
import com.Endurancer.manager.mapper.CommentMapper;
import com.Endurancer.manager.service.IBlogPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.Endurancer.manager.mapper.LikesMapper;
import com.Endurancer.manager.domain.Likes;
import com.Endurancer.manager.service.ILikesService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 点赞Service业务层处理
 *
 * @author Endurancer
 * @date 2025-02-04
 */
@Service
public class LikesServiceImpl implements ILikesService
{
    @Autowired
    private LikesMapper likesMapper;
    @Autowired
    private BlogPostMapper blogPostMapper;
    @Autowired
    private CommentMapper commentsMapper;

    /**
     * 查询点赞
     *
     * @param likeId 点赞主键
     * @return 点赞
     */
    @Override
    public Likes selectLikesByLikeId(Long likeId)
    {
        return likesMapper.selectLikesByLikeId(likeId);
    }

    /**
     * 查询点赞列表
     *
     * @param likes 点赞
     * @return 点赞
     */
    @Override
    public List<Likes> selectLikesList(Likes likes)
    {
        return likesMapper.selectLikesList(likes);
    }

    /**
     * 新增点赞
     *
     * @param likes 点赞
     * @return 结果
     */
    @Override
    public int insertLikes(Likes likes)
    {
        likes.setCreateTime(DateUtils.getNowDate());
        return likesMapper.insertLikes(likes);
    }

    /**
     * 修改点赞
     *
     * @param likes 点赞
     * @return 结果
     */
    @Override
    public int updateLikes(Likes likes)
    {
        likes.setUpdateTime(DateUtils.getNowDate());
        return likesMapper.updateLikes(likes);
    }

    /**
     * 批量删除点赞
     *
     * @param likeIds 需要删除的点赞主键
     * @return 结果
     */
    @Override
    public int deleteLikesByLikeIds(Long[] likeIds)
    {
        return likesMapper.deleteLikesByLikeIds(likeIds);
    }

    /**
     * 删除点赞信息
     *
     * @param likeId 点赞主键
     * @return 结果
     */
    @Override
    public int deleteLikesByLikeId(Long likeId)
    {
        return likesMapper.deleteLikesByLikeId(likeId);
    }

    @Transactional
    public Boolean isLike(LikesDto likesDto) {
        boolean isLiked;
        switch (likesDto.getModule()) {
            case "blog":
                isLiked = handleBlogLike(likesDto);
                break;
            case "comment":
                isLiked = handleCommentLike(likesDto);
                break;
            // 可以添加更多的case来处理其他模块
            default:
                throw new IllegalArgumentException("Unsupported module: " + likesDto.getModule());
        }
        return isLiked;
    }

    private boolean handleBlogLike(LikesDto likesDto) {
        BlogPost blogPost = blogPostMapper.selectBlogPostByPostId(likesDto.getFid());
        Likes dbLikes = likesMapper.selectUserLike(likesDto);

        if (dbLikes == null) {
            Likes like = new Likes();
            BeanUtil.copyProperties(likesDto, like);
            likesMapper.insertLikes(like);
            blogPost.setLikesCount(blogPost.getLikesCount() + 1);
            blogPostMapper.updateBlogPost(blogPost);
            return true;
        } else {
            likesMapper.deleteLikesByLikeId(dbLikes.getLikeId());
            blogPost.setLikesCount(blogPost.getLikesCount() - 1);
            blogPostMapper.updateBlogPost(blogPost);
            return false;
        }
    }

    private boolean handleCommentLike(LikesDto likesDto) {
        Comment comment = commentsMapper.selectCommentByCommentId(likesDto.getFid());
        Likes dbLikes = likesMapper.selectUserLike(likesDto);

        if (dbLikes == null) {
            Likes like = new Likes();
            BeanUtil.copyProperties(likesDto, like);
            likesMapper.insertLikes(like);
            comment.setLikes(comment.getLikes() + 1);
            commentsMapper.updateComment(comment);
            return true;
        } else {
            likesMapper.deleteLikesByLikeId(dbLikes.getLikeId());
            comment.setLikes(comment.getLikes() - 1);
            commentsMapper.updateComment(comment);
            return false;
        }
    }


    public List<LikesVo> selectUserLikes(LikesDto likesDto) {
        // 从数据库中查询点赞记录
        List<Likes> likes = likesMapper.selectUserLikes(likesDto);

        // 创建一个映射，用于快速查找点赞记录
        Map<Long, Likes> likesMap = likes.stream()
                .collect(Collectors.toMap(Likes::getFid, like -> like));

        // 遍历fids，构建LikesVo列表
        List<LikesVo> likesVoList = new ArrayList<>();
        for (Long fid : likesDto.getFids()) {
            LikesVo likesVo = new LikesVo();
            likesVo.setCommentId(fid.intValue()); // 假设commentId与fid类型一致，否则需要转换
            likesVo.setIsLiked(likesMap.containsKey(fid)); // 如果映射中包含fid，则表示已点赞
            likesVoList.add(likesVo);
        }

        return likesVoList;
    }

}
